package com.muyu.payment.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

/**
 * @author <a href="mailto:3546903628@qq.com">w1102</a>
 * @version 1.0
 * @description:
 * @date 2025/9/14 16:47
 */
@Data
@Component
@ConfigurationProperties(prefix = "alipay" ,ignoreInvalidFields = true)
public class AliPayConfig {
    /**
     * 獲取地址
     */
    private String appId ;
    /**
     * 商戶私鑰
     */
    private String appPrivateKey;
    
    /**
     * 支付寶公鑰
     */
    private String alipayPublicKey;
    /**
     * 服務器異步通知
     */
    private String notifyUrl;
    /**
     * 頁面跳轉同步通知頁面
     */
    private String returnUrl;
    /**
     * 支付寶官網
     */
    private String serverUrl;
    @PostConstruct
    public void init(){
        // 设置参数(全局只设置一次)
        Config config = new Config();
        config.protocol = "https";
        // 使用沙箱环境地址
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";
        config.signType = "RSA2";
        // 使用注入的配置值
        config.appId = this.appId;
        config.merchantPrivateKey = this.appPrivateKey;
        config.alipayPublicKey = this.alipayPublicKey;
        config.notifyUrl = this.notifyUrl;
//        // 设置编码
//        config.charset = "UTF-8";
//        // 设置公钥证书模式为false（使用普通公钥模式）
//        config.isCertOpen = false;

        try {
            Factory.setOptions(config);
            System.out.println("支付宝沙箱环境初始化成功");
        } catch (Exception e) {
            System.err.println("支付宝沙箱环境初始化失败: " + e.getMessage());
            throw new RuntimeException("支付宝沙箱环境初始化失败", e);
        }
    }
}
